package xyz.lovegu.server.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.lovegu.server.bus.dos.Health;
import xyz.lovegu.server.bus.mapper.HealthMapper;
import xyz.lovegu.server.bus.service.HealthService;
import xyz.lovegu.server.bus.vos.HealthVo;
import xyz.lovegu.server.sys.common.DataGridView;
import xyz.lovegu.server.sys.common.ResultObj;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author 老顾
 * @title: HealthServiceImpl
 * @projectName campus-prevention-system
 * @email: 1437594522@qq.com
 * @date 2022/5/10 11:00
 */
@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health> implements HealthService {

    /**
     * 增加健康填报表
     *
     * @param healthVo
     * @return
     */
    @Override
    public ResultObj addHealth(HealthVo healthVo) {
        try {
            Health health = new Health();
            BeanUtils.copyProperties(healthVo,health);
            this.save(health);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 批量删除健康表
     *
     * @param healthVo
     * @return
     */
    @Override
    public ResultObj deleteBatchHealth(HealthVo healthVo) {
        try {
            Collection<Serializable> list = new ArrayList<>(Arrays.asList(healthVo.getIds()));
            this.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询某一用户的健康填报表
     *
     * @param healthVo
     * @return
     */
    @Override
    public Object getHealthByUserId(HealthVo healthVo) {
        LambdaQueryWrapper<Health> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Health::getUserId,healthVo.getUserId());
        List<Health> list = this.list(lambdaQueryWrapper);
        for (Health health : list) {
            System.out.println(health.toString());
        }
        return new DataGridView(list);
    }

    /**
     * 获取所有健康填报表
     *
     * @param healthVo
     * @return
     */
    @Override
    public Object getAllHealth(HealthVo healthVo) {
        IPage<Health> page = new Page<>(healthVo.getPage(),healthVo.getLimit());
        LambdaQueryWrapper<Health> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(healthVo.getStartTime()!=null,Health::getCreateTime,healthVo.getStartTime());
        lambdaQueryWrapper.eq(healthVo.getEndTime()!=null,Health::getCreateTime,healthVo.getEndTime());
        lambdaQueryWrapper.orderByDesc(Health::getCreateTime);
        this.page(page,lambdaQueryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
